﻿using System;
using System.Web.UI;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;

using SiteUnity.Layers.Entity.General;
using SiteUnity.Layers.Application;
using SiteUnity.Frame.Common.Tools.Class;
using SiteUnity.Frame.Common.UITools;
using SiteUnity.Layers.Application.BaseClasses;

namespace SiteUnity.Frame.Modules
{
   [ToolboxData("<{0}:SubMenu runat=server></{0}:SubMenu>")]
    public class SubMenu : BaseWebControl
   {
        private const string ListCssClass = "SubMenuListCssClass";
        private const string SelectedClass = "selected";

        public bool HasUrl { get; set; }
        public string RelatedPageGalleryID { get; set; }
        public List<PageType> PageTypes { get; set; }
        public View View { get; set; }
        public string PageUrl { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (PageTypes == null && string.IsNullOrEmpty(PageUrl))
                PageTypes = ClassCollection.FindAll(SiteManager.PageTypes, "ParentID", SiteManager.Website.CurrentPage.typeID);
            else if (!string.IsNullOrEmpty(PageUrl))
                PageTypes = ClassCollection.FindAll(SiteManager.PageTypes, "ParentID", SiteManager.Website.SelectPageByUrl(PageUrl).typeID);
        }

        private void generateSubMenu(HtmlTextWriter output)
        {
            if (PageTypes.Count == 0)
            {
                this.Visible = false;
                return;
            }

            PageType all = new PageType();
            all.ID = 0;
            all.Name = "All";

            PageTypes.Insert(0,all);

            output.AddAttribute(HtmlTextWriterAttribute.Class, View.ToString());
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
            output.RenderBeginTag(HtmlTextWriterTag.Ul);

            bool isFirst = true;
            foreach (PageType type in PageTypes)
            {
                if (isFirst)
                {
                    output.AddAttribute(HtmlTextWriterAttribute.Class, SelectedClass);
                    isFirst = false;
                }
                output.RenderBeginTag(HtmlTextWriterTag.Li);

                if (HasUrl)
                    output.AddAttribute(HtmlTextWriterAttribute.Href, SiteManager.Website.CurrentPage.ParentPage.Url + "/" + type.Name);
                else
                    output.AddAttribute(HtmlTextWriterAttribute.Href, "#");

                output.AddAttribute(HtmlTextWriterAttribute.Title, type.Name);
                output.RenderBeginTag(HtmlTextWriterTag.A);
                output.Write(type.Name);
                output.RenderEndTag();
                
                output.RenderEndTag();
            }

            output.RenderEndTag();

        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!string.IsNullOrEmpty(RelatedPageGalleryID))
                Page.ClientScript.RegisterClientScriptBlock(typeof(System.Web.UI.Page), "readySubMenu", string.Format("var work = '#{0}'; var subMenu = '#{1}';", RelatedPageGalleryID, this.ID), true);
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            StringBuilder sb = new StringBuilder();
            HtmlTextWriter htw = new HtmlTextWriter(new StringWriter(sb, CultureInfo.InvariantCulture));
            generateSubMenu(htw);

            output.Write(sb.ToString());
        }
    }
}
